#include "view.h"
#include "chart/chartscene.h"
#include "chart/chartwindow.h"
#include <QStandardItem>
#include <QDebug>
#include <QColor>
#include <QDateTime>
#include "qwt_plot_marker.h"
#include "qwt_text.h"

View::View(QObject *parent) :
    QStandardItemModel(parent)
{
    chart=new ChartWindow();
    graph=new ChartScene(parent);

    /*signal, if a cell value changed*/
    connect(this,SIGNAL(valueChangedSignal(QStandardItem*,int)),this,SLOT(valueChanged(QStandardItem*,int)));
    /*signal, if a new stock was added -> add it to the view*/
    connect(this,SIGNAL(newValueSignal(QString,QList<QStandardItem*>)),this,SLOT(newValue(QString,QList<QStandardItem*>)));
}

View::~View()
{
    delete chart;
}

/*Init the model: create header items an attach to every cell a QStandardItem from stock*/
void View::initModel(const QStringList &hHeader, const QStringList &vHeader)
{
    if(!vHeader.isEmpty())
        vHeaderStr_db=vHeader;
    else
        vHeaderStr_db=getStockNames();  //get stock names from stock class

    if(columnCount()<hHeader.length())                           //increase columns
        insertColumns(0,hHeader.length()-columnCount());

    if(rowCount()<vHeaderStr_db.length())                              //increase rows
        insertRows(0,vHeaderStr_db.length()-rowCount());

    //cols:
    for(int i=0;i<hHeader.length();i++)                                 //set Horizontal header
    {
        setHeaderData(i, Qt::Horizontal,hHeader.at(i));
        //rows:
        for(int z=0;z<vHeaderStr_db.length();z++)                                 //set vertical header
        {
            setHeaderData(z, Qt::Vertical,vHeaderStr_db.at(z));
            //qDebug()<<"Attaching item("<<z<<","<<i<<")";
            /*attach the QStandardItem from stocks to the cell of the model*/
            setItem(z,i,getItem(z,i));
        }
    }
}

void View::newValue(const QString &stockName, QList<QStandardItem*> item)
{
    /*add a row and attach QStandardItems to each column*/
    appendRow(item);
    qDebug()<<"Adde Zeile "<<rowCount()<<" mit items( "<<item.count()<<" ): "<<item;

    /*set the stock name*/
    setHeaderData(rowCount()-1,Qt::Vertical,stockName);
}

void View::show(QTableView *view)
{
    if(view->disconnect(SIGNAL(doubleClicked(QModelIndex))))
        qDebug()<<"Diconnected object view.";
    else
        qDebug()<<"Could not disconnect object view";

    connect(view,SIGNAL(doubleClicked(QModelIndex)),this,SLOT(viewDoubleClicked(QModelIndex)));

    view->setModel(this);
    //view->adjustSize();
}

void View::hide()
{
    //view->disconnect(SIGNAL(doubleClicked(QModelIndex)));
    //view->setModel(new QStandardItemModel);
}



void View::setStockNames(const QStringList &stocks)
{
    vHeaderStr=stocks;
}

void View::setValueNames(const QStringList &vNames)
{
    hHeaderStr=vNames;
}

/*on double click on a cell -> pop up more information (chart,...)*/
void View::viewDoubleClicked(QModelIndex index)
{
   // qDebug()<<"DoubleClicked";
    QwtPlotMarker plotMarker;
    QList<QPointF> pointList;
    int row=index.row();
    qDebug()<<"DoubleClicked"<<row<<getStockNames();

    QString stockname=getStockNames().at(row);       //get the stock name

    qDebug()<<"Hole Daten";
    getColumnPairsFromDB(&pointList,"t10","l10",stockname,true);            //read all timestamps

    plotMarker.attach(chart->getPlot());
    graph->drawGraph(chart->getPlot(),pointList);                         //draw the graph with stock values
    chart->getPlot()->replot();
    chart->show();
}

/*slot, if a value changed.
  param:    item - the item, that changed
            type - increases,same,decreased*/
void View::valueChanged(QStandardItem *item, int type)
{
    QColor color;

    /*set cell color*/
    switch(type)
    {
    case 0: //value increased
        color=Qt::green;
    break;
    case 1: //value doesn't changed
        color=Qt::yellow;
        break;
    case 2: //value decreased
        color=Qt::red;
        break;
    }
    item->setData(color,Qt::BackgroundColorRole);

    /*convert unix time to a human readable time and write it to the cell item*/
    if(item->index().column()==getValSymbols().indexOf("t10") && item->data()!=QSTANDARDITEM_IS_NO_TIME)
    {
        QDateTime time;
        time.setTime_t(item->text().toDouble());
        if(time.isValid())
            item->setText(time.toString("hh:mm"));
    }
}
